package viettravel.guiandevent.place;

import java.util.ArrayList;

import viettravel.DAO.PlaceDAO;
import viettravel.common.Constants;
import viettravel.guiandevent.R;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RatingBar;
import android.widget.TextView;

public class ListPlaceAdapter extends BaseAdapter {

	private ArrayList<PlaceDAO> mRowPlace;
	private ArrayList<PlaceDAO> mFilterPlace;
	private Context mContext;
	
	public ListPlaceAdapter(ArrayList<PlaceDAO> _rowPlace, Context _context){
		mRowPlace = new ArrayList<PlaceDAO>();
		mRowPlace.addAll(_rowPlace);
		
		mFilterPlace = new ArrayList<PlaceDAO>();
		mFilterPlace.addAll(_rowPlace);
		mContext = _context;
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mFilterPlace.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mFilterPlace.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View _view, ViewGroup _viewGroup) {
		// TODO Auto-generated method stub
		View view = _view;
		if (view == null){
			LayoutInflater layoutInflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = layoutInflater.inflate(R.layout.row_place_activity, null);
        }
		Typeface tf = Typeface.createFromAsset(mContext.getAssets(),"fonts/utm_caviar.ttf");
		TextView placeName = (TextView)view.findViewById(R.id.placeName);
		placeName.setTypeface(tf);
		RatingBar placeRate = (RatingBar)view.findViewById(R.id.placeRating);
		
		PlaceDAO rowPlace = mFilterPlace.get(position);
		placeName.setText(rowPlace.getPlaceName());
		placeRate.setRating(rowPlace.getRate());                            
		return view;
	}

	@SuppressLint("DefaultLocale")
	public void filterData(String query){
		query = query.toLowerCase();
		mFilterPlace.clear();
		if(query.equals("")){
			mFilterPlace.addAll(mRowPlace);
		}else{
			ArrayList<PlaceDAO> newList = new ArrayList<PlaceDAO>();
			for(PlaceDAO rowPlace : mRowPlace){
				if(Constants.isContain(rowPlace.getPlaceName(), query))
					newList.add(rowPlace);
			}
			if(newList.size() > 0){
				mFilterPlace.addAll(newList);
			}
		}
		notifyDataSetChanged();
	}
	
	public void ascSort(){
		for(int i = 0; i < mFilterPlace.size(); i++){
			int max = i;
			PlaceDAO placeMaxRating = mFilterPlace.get(i);
			for(int j = i; j < mFilterPlace.size() - 1; j++){
				if(placeMaxRating.getRate() < mFilterPlace.get(j + 1).getRate()){
					placeMaxRating = mFilterPlace.get(j + 1);
					max = j + 1;
				}
			}
			PlaceDAO temp = mFilterPlace.get(i);
			mFilterPlace.set(i, placeMaxRating);
			mFilterPlace.set(max, temp);
		}
		notifyDataSetChanged();
	}
	
	public void decSort(){
		for(int i = 0; i < mFilterPlace.size(); i++){
			int min = i;
			PlaceDAO placeMinRating = mFilterPlace.get(i);
			for(int j = i; j < mFilterPlace.size() - 1; j++){
				if(placeMinRating.getRate() > mFilterPlace.get(j + 1).getRate()){
					placeMinRating = mFilterPlace.get(j + 1);
					min = j + 1;
				}
			}
			PlaceDAO temp = mFilterPlace.get(i);
			mFilterPlace.set(i, placeMinRating);
			mFilterPlace.set(min, temp);
		}
		notifyDataSetChanged();
	}
}
